from flask import Blueprint, request, session
from util.result import support_jsonp, JsonResult
from model.user import User

"""
#实例化蓝图对象 相当于controller
"""

user = Blueprint('user',__name__,url_prefix='/weixin')

@user.before_request
def before_user_equest():
    print('user拦截器拦截的请求参数是：'
          ,request.values
          ,request.url
          ,request.authorization
          ,request.base_url
          ,session)
    if 'userName' in session:
        print('userName in session')

@user.route('/login')
@support_jsonp
def login():
    request_params_dic = request.values
    userName = (request_params_dic.get('userName'))
    password = (request_params_dic.get('password'))

    if userName is None or password is None:
        return JsonResult.fail('用户名或者密码不能为空')

    boolean_fail = User.login(str(userName),str(password))

    if boolean_fail:
        return JsonResult.fail('用户不存在')

    # 登录成功先将用户名写进session
    session['userName'] = userName
    return JsonResult.success('登陆成功')

@user.route('/getAllUsers')
@support_jsonp
def getAllUsers():
    user_list = User.queryAllUsers()
    return  JsonResult.success(user_list)